/**
 * Created by 97203 on 2018/06/08.
 */
$(function () {
  var dept_datagrid = $("#dept_datagrid");
  var dept_dialog = $("#dept_dialog");
  var dept_form = $("#dept_form");
  dept_datagrid.datagrid({
    url: '/car/query.do',
    fitColumns: true,
    fit: true,
    pagination: true,    //分页条
    rownumbers: true,     //行号列
    singleSelect: true,
    toolbar: '#tb',
    onClickRow: function (index, row) {
      if (row.state) {
        $("#changeState_btn").linkbutton({
          text: '停用'
        })
      } else {
        $("#changeState_btn").linkbutton({
          text: '正常'
        })
      }
    },
    columns: [[
      {
        field: 'client', title: '车主姓名', width: 100, formatter: function (value) {
          return value ? value.name : "";
        }
      },
      {field: 'brand', title: '车辆品牌', width: 100},
      {field: 'model', title: '车辆型号', width: 100},
      {field: 'plateNumber', title: '车牌号码', width: 100},
      {field: 'fasDisplacement', title: '排气量/L', width: 100},
      {field: 'purchaseDate', title: '购买日期', width: 100},
      {field: 'valuation', title: '市场估价/万元', width: 100},
      {
        field: 'category', title: '分类', width: 100, formatter: function (value) {
          return value ? "<font color='green'>载人</font>" : "<font color='red'>载物</font>";
        }
      },
      {
        field: 'size', title: '车载大小', width: 100, formatter: function (value, row, index) {
          if (value == 0 && value == '') {
            return "大";
          } else if (value == 1) {
            return "中";
          } else {
            return "小";
          }
        }
      },
      {field: 'remarks', title: '备注信息', width: 100},
    ]]
  })
  dept_dialog.dialog({
    width: 300,
    height: 280,
    buttons: '#btn',
    closed: true,
    //在窗口关闭的时候触发事件
    onClose: function () {
      //清空表单数据
      dept_form.form('clear');
    }
  })

  var methodObj = {
    add: function () {
      //设置标题
      dept_dialog.dialog('setTitle', '新增部门');
      //打开弹出框
      dept_dialog.dialog('open');
    },
    edit: function () {

      var row = dept_datagrid.datagrid('getSelected');
      if (!row) {
        $.messager.alert('温馨提示', '请选中要编辑的数据！', 'warning');
        return;
      }
      if (row.client) {
        row["client.id"] = row.client.id;
      }
      if (row.client) {
        row["client.name"] = row.client.name;
      }
      //把数据回显到表单中
      dept_form.form('load', row);
      //设置标题
      dept_dialog.dialog('setTitle', '编辑部门');
      //打开弹出框
      dept_dialog.dialog('open');

    },
    changeState: function () {
      var row = dept_datagrid.datagrid('getSelected');
      if (!row) {
        $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
        return;
      }
      $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
        if (r) {
          $.get('/car/changeState.do', {id: row.id}, function (data) {
            if (data.success) {
              $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                dept_datagrid.datagrid('reload');
              });
            } else {
              $.messager.alert('温馨提示', data.msg, 'warning');
            }
          })
        }
      });
    },
    reload: function () {
      dept_datagrid.datagrid('reload');
    },
    save: function () {
      //提交ajax
      dept_form.form('submit', {
        url: '/car/saveOrUpdate.do',
        success: function (data) {
          data = $.parseJSON(data)
          if (data.success) {
            $.messager.alert('温馨提示', '保存成功！', 'info', function () {
              //关闭弹出框
              methodObj.cancel();
              //在dataGrid重新加载数据
              dept_datagrid.datagrid('reload');
            });
          } else {
            $.messager.alert('温馨提示', data.msg, 'warning');
          }
        }
      })
    },
    cancel: function () {
      dept_dialog.dialog('close');
    },
    query: function () {
      var keyword = $("[name=keyword]").val();
      dept_datagrid.datagrid('load', {
        keyword: keyword
      })
    }
  }
  $("a[data-cmd]").click(function () {
    //获取按钮上的data-cmd的值
    var methodName = $(this).data("cmd");
    methodObj[methodName]();
  })
})
